const fs = require('fs');

/**
 * write a file
 * @param path
 * @param information
 * @param type
 */
function write(path, information, type) {
  fs.writeFile(path, information, {flag: type || "a"}, (err) => {
    new Promise((resolve,reject)=>{
      !err ? resolve() : reject();
    }).then(
        ()=>{
          arguments[-1](1);
        }
        ,()=>{
          arguments[-1](0);
        }
    )
  });
}

/**
 *
 * @param path
 * @param buffer
 * @param callback
 */
const writeFileRecursive = function (path, buffer, callback) {
  let lastPath = path.substring(0, path.lastIndexOf("/"));
  fs.mkdir(lastPath, {recursive: true}, (err) => {
    if (err) return callback(err);
    fs.writeFile(path, buffer, function (err) {
      if (err) return callback(err);
      return callback(undefined);
    });
  });
};

module.exports={
  write:write,
  writeFileRecursive:writeFileRecursive
}